﻿namespace DomainServices.Tools
{
    using System;
    using System.ComponentModel.DataAnnotations;

    internal class RangeCustomAttributeBuilder : ValidationCustomAttributeBuilder
    {
        public override AttributeDeclaration GetAttributeDeclaration(Attribute attribute)
        {
            RangeAttribute validationAttribute = (RangeAttribute) attribute;
            AttributeDeclaration attributeDeclaration = new AttributeDeclaration(typeof(RangeAttribute));
            ValidationCustomAttributeBuilder.RegisterSharedResources(validationAttribute, attributeDeclaration);
            if ((validationAttribute.Minimum == null) || (validationAttribute.Minimum.GetType() == typeof(string)))
            {
                attributeDeclaration.ConstructorArguments.Add(validationAttribute.OperandType);
            }
            attributeDeclaration.ConstructorArguments.Add(validationAttribute.Minimum);
            attributeDeclaration.ConstructorArguments.Add(validationAttribute.Maximum);
            if (validationAttribute.ErrorMessage != null)
            {
                attributeDeclaration.NamedParameters.Add("ErrorMessage", validationAttribute.ErrorMessage);
            }
            if (validationAttribute.ErrorMessageResourceType != null)
            {
                attributeDeclaration.NamedParameters.Add("ErrorMessageResourceType", validationAttribute.ErrorMessageResourceType);
            }
            if (validationAttribute.ErrorMessageResourceName != null)
            {
                attributeDeclaration.NamedParameters.Add("ErrorMessageResourceName", validationAttribute.ErrorMessageResourceName);
            }
            return attributeDeclaration;
        }
    }
}

